﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages
{
    public class PatientList : BasePage
    {
       
        protected override void ShowPage()
        {
            base.ShowPage();
            if(ErrorCount > 0) return;
            
            HandleCommon();
            if(ErrorCount > 0) return;
            
            if(IsPost) HandlePost();
            else HandleGet();
        }

        //protected List<TopicInfo> TopicDataListHottest;
        //protected List<TopicInfo> TopicDataListLatest;
        protected List<UserInfo> UserDataListLatest;
        protected List<UserInfo> PatientDataList;
        protected List<UserInfo> PatientDataListActive;
        protected List<DiseaseInfo> DiseaseLst;
        protected List<TreatmentInfo> TreatmentLst;
        protected List<SymptomInfo> SymptomLst;
        protected List<DistrictInfo> ProvinceList;
        protected int PatientCount = 0;
        protected int PatientPageCount = 0;
        protected int PatientPageSize = 15;
        protected int PatientPageIndex = 0;
        protected string PatientPagingString;
        protected PatientSearchCondition cond = null;

        protected void HandleCommon()
        {
            this.SetTitle("病友列表");
            this.AddLocation("病友列表", "/PatientAll.aspx");
            DiseaseLst = DiseaseBLL.Instance.GetAllDiseaseByName("");
            TreatmentLst = TreatmentBLL.Instance.GetAllTreatmentByName("");
            SymptomLst = SymptomBLL.Instance.GetAllSymptomByName("");
            ProvinceList = DistrictBLL.Instance.GetDistrictListByParent(0);
        }
        
        protected void HandlePost()
        {
        }
            
#if GENDATA
        protected void HandleGet()
        {
            TopicDataListHottest = new List<TopicInfo>();
            for (int i = 0; i < 5; i++)
            {
                TopicInfo info = new TopicInfo();
                info.Id = i;
                info.Title = "最热话题最热话题";
                info.ReplyCount = 10;
                TopicDataListHottest.Add(info);
            }

            TopicDataListLatest = new List<TopicInfo>();
            for (int i = 0; i < 5; i++)
            {
                TopicInfo info = new TopicInfo();
                info.Id = i;
                info.Title = "最新话题最新话题";
                info.ReplyCount = 10;
                TopicDataListLatest.Add(info);
            }

            UserDataListLatest = new List<UserInfo>();
            for (int i = 0; i < 5; i++)
            {
                UserInfo info = new UserInfo();
                info.Id = i;
                info.Avatar = "/support/dnt/avatars/common/1.jpg";
                info.UserName = "用户名";
                UserDataListLatest.Add(info);
            }
            PatientPageIndex = RequestTools.GetQueryInt("page", 1);

            PatientDataList = new List<UserInfo>();
            for (int i = 1; i < 10; i++)
            {
                UserInfo info = new UserInfo();
                info.Id = 100 * i + i;
                info.UserName = "病人";
                info.Intro = "病人简介病人简介病人简介病人简介病人简介病人简介病人简介";
                info.Avatar = "/support/dnt/avatars/common/1.jpg";
                info.Age = 20;
                info.Gender = "男";
                info.DiseaseId = 10;
                info.DiseaseName = "糖尿病";
                PatientDataList.Add(info);
            }
            PatientPageCount = Utils.GetPageCount(PatientCount, 10);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PatientPagingString = this.GetPagingString(PatientPageIndex, PatientPageCount,
                "/PatientList.aspx" + query, "page");

        }

#else
        protected void HandleGet()
        {
            int count = 6;
            //TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(count);
            //TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(count);
            UserDataListLatest = PatientBLL.Instance.GetLastestPatient(count);
            PatientDataListActive = PatientBLL.Instance.GetPatientDataListActive(count);

            cond = new PatientSearchCondition();
            cond.AgeStart = RequestTools.GetQueryInt("agefrom",0);
            cond.AgeEnd = RequestTools.GetQueryInt("ageto",0);
            cond.ProvinceId = RequestTools.GetQueryInt("province", 0);
            cond.CityId = RequestTools.GetQueryInt("city", 0);
            cond.TreatmentId = RequestTools.GetQueryInt("trmid",0);
            cond.TreatmentName = RequestTools.GetQueryString("trmname");
            cond.SymptomId = RequestTools.GetQueryInt("symid", 0);
            cond.SymptomName = RequestTools.GetQueryString("symname");
            cond.DiseaseId = RequestTools.GetQueryInt("dszid",0);
            cond.DiseaseName = RequestTools.GetQueryString("dszname");
            cond.PatientName = RequestTools.GetQueryString("name");
            cond.Gender = RequestTools.GetQueryString("gender");
            cond.IsAdvanceSearch = RequestTools.GetInt("advance",0);
            cond.SymptomDegree = RequestTools.GetInt("symptomdegree", 0);
            cond.StartDosage = RequestTools.GetInt("startdosage", 0);
            cond.EndDosage = RequestTools.GetInt("enddosage", 0);
            cond.IsQueryCurrent = RequestTools.GetInt("isquerycurrent", 1);

            PatientPageIndex = RequestTools.GetQueryInt("page", 1);
            PatientDataList = PatientBLL.Instance.GetPatientList(
                PatientPageSize,PatientPageIndex,
                cond, out PatientCount);

            PatientPageCount = Utils.GetPageCount(PatientCount, PatientPageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PatientPagingString = this.GetPagingString(PatientPageIndex, PatientPageCount,
                "/PatientList.aspx" + query, "page");
        }      
#endif

        protected string GetCondStr(PatientSearchCondition cond)
        {
            string condStr = String.Empty;
            bool firstCond = true;

            if (cond == null)
            {
                return string.Empty;
            }

            if (cond.DiseaseId > 0)
            {
                condStr += DiseaseBLL.Instance.GetDiseaseInfo(cond.DiseaseId).Name;
                firstCond = false;
            }

            if (cond.SymptomId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                } 
                condStr += SymptomBLL.Instance.GetSymptomInfo(cond.SymptomId).Name;
                firstCond = false;
            }

            if (cond.TreatmentId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += TreatmentBLL.Instance.GetTreatmentInfo(cond.TreatmentId).Name;
                firstCond = false;
            }

            if (cond.TreatmentName != "")
            {
                if (!firstCond)
                {
                    condStr += "+";
                }
                condStr += cond.TreatmentName;
                firstCond = false;
            }

            if (cond.ProvinceId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += DistrictBLL.Instance.GetDistrict(cond.ProvinceId).Name;
                firstCond = false;
            }

            if (cond.CityId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += DistrictBLL.Instance.GetDistrict(cond.CityId).Name;
                firstCond = false;
            }

            if (cond.Gender.Length > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += cond.Gender;
                firstCond = false;
            }

            if (cond.AgeStart > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += " 年龄大于" + cond.AgeStart + "岁";
                firstCond = false;                
            }

            if (cond.AgeEnd > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += " 年龄小于" + cond.AgeEnd + "岁";
                firstCond = false;
            }

            if (cond.PatientName.Length > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += cond.PatientName;
            }

            return condStr;
        }
    }
}